package com.ruoyi.web.controller.recipe;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.recipe.domain.RecipeCategoryDish;
import com.ruoyi.recipe.domain.RecipeDish;
import com.ruoyi.recipe.domain.RecipeTagCategory;
import com.ruoyi.recipe.service.IRecipeCategoryDishService;
import com.ruoyi.recipe.service.IRecipeDishService;
import com.ruoyi.recipe.service.IRecipeTagService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.recipe.domain.RecipeCategory;
import com.ruoyi.recipe.service.IRecipeCategoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 食谱类别Controller
 * 
 * @author ruoyi
 * @date 2024-11-10
 */
@Controller
@RequestMapping("/recipe/category")
public class RecipeCategoryController extends BaseController {
    private String prefix = "recipe/category";

    @Autowired
    private IRecipeTagService recipeTagService;

    @Autowired
    private IRecipeCategoryService recipeCategoryService;

    @Autowired
    private IRecipeDishService recipeDishService;

    @Autowired
    private IRecipeCategoryDishService recipeCategoryDishService;

    @RequiresPermissions("recipe:category:view")
    @GetMapping()
    public String category() {
        return prefix + "/category";
    }

    /**
     * 查询食谱类别列表
     */
    @RequiresPermissions("recipe:category:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecipeCategory recipeCategory) {
        startPage();
        List<RecipeCategory> list = recipeCategoryService.selectCategoryList(recipeCategory);
        return getDataTable(list);
    }

    /**
     * 导出食谱类别列表
     */
    @RequiresPermissions("recipe:category:export")
    @Log(title = "食谱类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RecipeCategory recipeCategory) {
        List<RecipeCategory> list = recipeCategoryService.selectCategoryList(recipeCategory);
        ExcelUtil<RecipeCategory> util = new ExcelUtil<RecipeCategory>(RecipeCategory.class);
        return util.exportExcel(list, "食谱类别数据");
    }

    /**
     * 新增食谱类别
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存食谱类别
     */
    @RequiresPermissions("recipe:category:add")
    @Log(title = "食谱类别", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecipeCategory recipeCategory) {
        return toAjax(recipeCategoryService.insertCategory(recipeCategory));
    }

    /**
     * 修改食谱类别
     */
    @RequiresPermissions("recipe:category:edit")
    @GetMapping("/edit/{categoryId}")
    public String edit(@PathVariable("categoryId") Long categoryId, ModelMap mmap) {
        RecipeCategory recipeCategory = recipeCategoryService.selectCategoryById(categoryId);
        mmap.put("recipeCategory", recipeCategory);
        return prefix + "/edit";
    }

    /**
     * 修改保存食谱类别
     */
    @RequiresPermissions("recipe:category:edit")
    @Log(title = "食谱类别", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecipeCategory recipeCategory) {
        return toAjax(recipeCategoryService.updateCategory(recipeCategory));
    }

    /**
     * 删除食谱类别
     */
    @RequiresPermissions("recipe:category:remove")
    @Log(title = "食谱类别", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(recipeCategoryService.deleteCategoryByIds(ids));
    }

    /**
     * 校验食谱类别名称
     */
    @PostMapping("/checkCategoryNameUnique")
    @ResponseBody
    public boolean checkCategoryNameUnique(RecipeCategory recipeCategory) {
        return recipeCategoryService.checkCategoryNameUnique(recipeCategory);
    }

    /**
     * 分配菜品
     */
    @RequiresPermissions("recipe:category:edit")
    @GetMapping("/allocateDish/{categoryId}")
    public String allocateDish(@PathVariable("categoryId") Long categoryId, ModelMap mmap) {
        mmap.put("category", recipeCategoryService.selectCategoryById(categoryId));
        return prefix + "/allocateDish";
    }

    /**
     * 查询已分配类别列表
     */
    @RequiresPermissions("recipe:category:list")
    @PostMapping("/allocateDish/allocatedList")
    @ResponseBody
    public TableDataInfo allocatedList(RecipeDish dish) {
        startPage();
        List<RecipeDish> list = recipeDishService.selectAllocatedList(dish);
        return getDataTable(list);
    }

    /**
     * 选择菜品
     */
    @GetMapping("/allocateDish/selectDish/{categoryId}")
    public String selectDish(@PathVariable("categoryId") Long categoryId, ModelMap mmap) {
        mmap.put("category", recipeCategoryService.selectCategoryById(categoryId));
        return prefix + "/selectDish";
    }

    /**
     * 查询未分配类别列表
     */
    @RequiresPermissions("recipe:category:list")
    @PostMapping("/allocateDish/unallocatedList")
    @ResponseBody
    public TableDataInfo unallocatedList(RecipeDish dish) {
        startPage();
        List<RecipeDish> list = recipeDishService.selectUnallocatedList(dish);
        return getDataTable(list);
    }

    /**
     * 批量分类
     */
    @RequiresPermissions("recipe:category:edit")
    @Log(title = "菜品分类", businessType = BusinessType.GRANT)
    @PostMapping("/allocateDish/selectAll")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult selectAllocateCategoryAll(Long categoryId, String dishIds) {
        int rows = recipeCategoryDishService.batchInsertRecipeCategoryDish(categoryId, dishIds);
        if (rows > 0) {
            recipeDishService.updateDishTitle(dishIds);
        }
        return toAjax(rows);
    }

    /**
     * 取消分类
     */
    @RequiresPermissions("recipe:category:edit")
    @Log(title = "菜品分类", businessType = BusinessType.GRANT)
    @PostMapping("/allocateDish/cancel")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult cancelAllocateCategory(RecipeCategoryDish recipeCategoryDish) {
        int rows = recipeCategoryDishService.deleteRecipeCategoryDish(recipeCategoryDish);
        if (rows > 0) {
            recipeDishService.updateDishTitle(recipeCategoryDish.getDishId().toString());
        }
        return toAjax(rows);
    }

    /**
     * 批量取消菜品分类
     */
    @RequiresPermissions("recipe:category:edit")
    @Log(title = "菜品分类", businessType = BusinessType.GRANT)
    @PostMapping("/allocateDish/cancelAll")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult cancelAllocateDish(Long categoryId, String dishIds) {
        int rows = recipeCategoryDishService.deleteRecipeCategoryDishByKeys(categoryId, dishIds);
        if (rows > 0) {
            recipeDishService.updateDishTitle(dishIds);
        }
        return toAjax(rows);
    }
}
